package com.cspm.gen.service.impl;

import com.cspm.common.proxy.DynamicDataSource;
import com.cspm.gen.dao.SystemGeneratorDao;
import com.cspm.gen.enums.DriverCLassEnum;
import com.cspm.gen.service.SystemGeneratorService;
import com.cspm.gen.util.GenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * Created by CHQIU on 2017-04-15.
 */
@Service
public class SystemGeneratorServiceImpl implements SystemGeneratorService {
    @Resource
    private SystemGeneratorDao systemGeneratorDao;

    public SystemGeneratorDao getSystemGeneratorDao() {
        return systemGeneratorDao;
    }

    @Resource
    public void setSystemGeneratorDao(SystemGeneratorDao systemGeneratorDao) {
        this.systemGeneratorDao = systemGeneratorDao;
    }

    /**
     * 通过表名模糊查询
     *
     * @param tableName 表名
     * @param page      当前页面
     * @param rows      当页行数
     * @return 数据库表列表
     */
    @Override
    public PageInfo<Map<String, Object>> queryList(String tableName, Integer page, Integer rows) {
        // 分页查询
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = systemGeneratorDao.queryList(tableName);
        return new PageInfo<>(list);
    }

    /**
     * 查询表信息
     *
     * @param tableName 待查询表
     * @return 表信息
     */
    @Override
    public Map<String, String> queryTable(String tableName) {
        return systemGeneratorDao.queryTable(tableName);
    }

    /**
     * 查询表字段信息
     *
     * @param tableName 待查询表
     * @return 表字段信息
     */
    @Override
    public List<Map<String, String>> queryColumns(String tableName) {
        return systemGeneratorDao.queryColumns(tableName);
    }

    /**
     * 生成代码
     *
     * @param tableNames 生成代码的表
     * @return 字节码
     */
    @Override
    public byte[] generatorCode(String[] tableNames) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            //生成代码
            GenUtils.generatorCode(table, columns, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    /**
     * 生成代码
     *
     * @param codePackage 包名
     * @param author      创建人
     * @param tableNames  生成代码的表
     * @return 字节码
     */
    @Override
    public byte[] generatorCode(String codePackage, String author, String[] tableNames) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            //生成代码
            GenUtils.generatorCode(codePackage, author, table, columns, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    /**
     * 生成全部表代码
     *
     * @param codePackage 包名
     * @param author      创建人
     * @return 字节码
     */
    @Override
    public byte[] generatorCodeAll(String codePackage, String author) throws Exception {
        List<Map<String, Object>> list = systemGeneratorDao.queryList(null);

        List<String> tableNameList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            tableNameList.add(map.get("tableName").toString());
        }
        String[] tableNames = tableNameList.toArray(new String[tableNameList.size()]);
        return generatorCode(codePackage, author, tableNames);
    }

    /**
     * 连接数据库
     *
     * @param driverCLassEnum 数据库驱动
     * @param server          服务器地址
     * @param port            端口号
     * @param database        数据库名
     * @param username        登录名
     * @param password        密码
     * @return 连接是否成功
     */
    @Override
    public String connectDatabase(DriverCLassEnum driverCLassEnum, String server, Integer port, String database, String username, String password) {
        DynamicDataSource.setCustomerDataSource(driverCLassEnum, server, port, database, username, password);
        return "成功！";
    }
}
